LAPLACE
Overview
The LAPLACE function computes various properties of the Laplace distribution, a continuous probability distribution also known as the double exponential distribution. Named after Pierre-Simon Laplace, who first published it in 1774, the distribution can be thought of as two exponential distributions spliced together back-to-back along the x-axis.
The Laplace distribution is characterized by two parameters: a location parameter \mu that determines the peak of the distribution, and a scale parameter b > 0 (sometimes called “diversity”) that controls the spread. The probability density function (PDF) is defined as:
f(x \mid \mu, b) = \frac{1}{2b} \exp\left(-\frac{|x - \mu|}{b}\right)
Unlike the normal distribution, which uses squared differences from the mean, the Laplace distribution uses the absolute difference. This results in a sharper peak at the mode and heavier tails than the normal distribution, making it particularly useful for modeling data with outliers or extreme values.
The cumulative distribution function (CDF) has a closed-form expression:
F(x) = \frac{1}{2} + \frac{1}{2} \text{sgn}(x - \mu) \left(1 - \exp\left(-\frac{|x - \mu|}{b}\right)\right)
Key statistical properties include: mean and median equal to \mu, variance of 2b^2, and standard deviation of b\sqrt{2}. The distribution has zero skewness and excess kurtosis of 3.
The Laplace distribution has important applications in differential privacy (adding Laplacian noise to protect sensitive data), regression analysis (least absolute deviations estimation), Bayesian statistics (as a prior in LASSO regularization), and signal processing (modeling speech and image compression coefficients). In finance, it addresses skewness and kurtosis issues that arise when using normal distributions for option pricing.
This implementation uses the scipy.stats.laplace module from SciPy and supports multiple calculation methods including PDF, CDF, inverse CDF (quantile function), survival function, mean, median, variance, and standard deviation.
This example function is provided as-is without any representation of accuracy.
Excel Usage
=LAPLACE(value, loc, scale, laplace_method)
value(float, optional, default: null): Input value for the distribution. Required for pdf, cdf, icdf, sf, isf methods.loc(float, optional, default: 0): Location parameter of the distribution.scale(float, optional, default: 1): Scale parameter of the distribution. Must be greater than 0.laplace_method(str, optional, default: “pdf”): The method to compute (pdf, cdf, icdf, sf, isf, mean, median, var, std).
Returns (float): Result of the requested method, or str error message if input is invalid.
Examples
Example 1: PDF at x=1
Inputs:
| value | loc | scale | laplace_method |
|---|---|---|---|
| 1 | 0 | 1 |
Excel formula:
=LAPLACE(1, 0, 1, "pdf")
Expected output:
0.1839
Example 2: CDF at x=1
Inputs:
| value | loc | scale | laplace_method |
|---|---|---|---|
| 1 | 0 | 1 | cdf |
Excel formula:
=LAPLACE(1, 0, 1, "cdf")
Expected output:
0.8161
Example 3: Inverse CDF at q=0.81606
Inputs:
| value | loc | scale | laplace_method |
|---|---|---|---|
| 0.81606 | 0 | 1 | icdf |
Excel formula:
=LAPLACE(0.81606, 0, 1, "icdf")
Expected output:
1
Example 4: Mean of the distribution
Inputs:
| loc | scale | laplace_method |
|---|---|---|
| 0 | 1 | mean |
Excel formula:
=LAPLACE(0, 1, "mean")
Expected output:
0
Python Code
from scipy.stats import laplace as scipy_laplace
import math
def laplace(value=None, loc=0, scale=1, laplace_method='pdf'):
"""
Laplace distribution function supporting multiple methods.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.laplace.html
This example function is provided as-is without any representation of accuracy.
Args:
value (float, optional): Input value for the distribution. Required for pdf, cdf, icdf, sf, isf methods. Default is None.
loc (float, optional): Location parameter of the distribution. Default is 0.
scale (float, optional): Scale parameter of the distribution. Must be greater than 0. Default is 1.
laplace_method (str, optional): The method to compute (pdf, cdf, icdf, sf, isf, mean, median, var, std). Valid options: PDF, CDF, ICDF, SF, ISF, Mean, Median, Var, Std. Default is 'pdf'.
Returns:
float: Result of the requested method, or str error message if input is invalid.
"""
valid_methods = {'pdf', 'cdf', 'icdf', 'sf', 'isf', 'mean', 'median', 'var', 'std'}
if not isinstance(laplace_method, str):
return "Invalid input: laplace_method must be a string."
method = laplace_method.lower()
if method not in valid_methods:
return f"Invalid method: {laplace_method}. Must be one of {', '.join(sorted(valid_methods))}."
try:
loc = float(loc)
scale = float(scale)
except (ValueError, TypeError):
return "Invalid input: loc and scale must be numbers."
if scale <= 0:
return "Invalid input: scale must be > 0."
dist = scipy_laplace(loc, scale)
# Methods that require value
if method in ['pdf', 'cdf', 'icdf', 'sf', 'isf']:
if value is None:
return f"Invalid input: missing required argument 'value' for method '{method}'."
try:
value = float(value)
except (ValueError, TypeError):
return "Invalid input: value must be a number."
try:
if method == 'pdf':
result = dist.pdf(value)
elif method == 'cdf':
result = dist.cdf(value)
elif method == 'sf':
result = dist.sf(value)
elif method == 'isf':
if not (0 <= value <= 1):
return "Invalid input: value (probability) must be between 0 and 1 for isf."
result = dist.isf(value)
elif method == 'icdf':
if not (0 <= value <= 1):
return "Invalid input: value (probability) must be between 0 and 1 for icdf."
result = dist.ppf(value)
except Exception as e:
return f"scipy.stats.laplace error: {e}"
if isinstance(result, float):
if math.isnan(result):
return "Result is NaN (not a number)"
if math.isinf(result):
return "inf" if result > 0 else "-inf"
return result
# Methods that do not require value
try:
if method == 'mean':
result = dist.mean()
elif method == 'median':
result = dist.median()
elif method == 'var':
result = dist.var()
elif method == 'std':
result = dist.std()
except Exception as e:
return f"scipy.stats.laplace error: {e}"
if isinstance(result, float):
if math.isnan(result):
return "Result is NaN (not a number)"
if math.isinf(result):
return "inf" if result > 0 else "-inf"
return result